//
//  DNCryingManagerTests.m
//  DanoneEA
//
//  Created by Senela on 16/10/31.
//  Copyright © 2016年 翔傲科技. All rights reserved.
//

#import <XCTest/XCTest.h>
#import "DNCryingManager.h"
#import "NSDate+Utils.h"
#import "DNCryingDao.h"

@interface DNCryingManagerTests : XCTestCase
{
    DNCryingModel *cryingInfo;
    DNCryingModel *cryingInfo1;
}
@end

@implementation DNCryingManagerTests

- (void)setUp {
    [super setUp];
    // Put setup code here. This method is called before the invocation of each test method in the class.
 
    cryingInfo = [[DNCryingModel alloc] init];
    cryingInfo.appId =[NSString stringWithFormat:@"%f", [[NSDate date] timeIntervalSince1970]];
    cryingInfo.createTime =[NSString stringWithFormat:@"%.0f", [[NSDate date] timeIntervalSince1970]];

    //TODO: format to be confirm
    cryingInfo.ddat = [NSDate currentTimeString];
    cryingInfo.timeOfDay = TimeOfDay_Morning;
    cryingInfo.cryInyn = @"Y";
    cryingInfo.crySttime = @"09:00";
    cryingInfo.cryEntime = @"10:00";
    
    
    cryingInfo1 = [[DNCryingModel alloc] init];
    cryingInfo1.appId =[NSString stringWithFormat:@"%f", [[NSDate date] timeIntervalSince1970]];
    cryingInfo1.createTime =[NSString stringWithFormat:@"%.0f", [[NSDate date] timeIntervalSince1970]];
    
    //TODO: format to be confirm
    cryingInfo1.ddat = [NSDate currentTimeString];
    cryingInfo1.timeOfDay = TimeOfDay_Morning;
    cryingInfo1.cryInyn = @"N";
    cryingInfo1.crySttime = @"10:00";
    cryingInfo1.cryEntime = @"11:00";
}

- (void)tearDown {
    // Put teardown code here. This method is called after the invocation of each test method in the class.
    [super tearDown];
}

- (void)testExample {
    // This is an example of a functional test case.
    // Use XCTAssert and related functions to verify your tests produce the correct results.
}

- (void)testPerformanceExample {
    // This is an example of a performance test case.
    [self measureBlock:^{
        // Put the code you want to measure the time of here.
    }];
}

-(void)test1AddSuc
{
    [[DNDataBase shareInstance] clearAllTableData];
    
    // 场景一：成功－新增 model
    BOOL ret = [[DNCryingManager sharedInstance] isOverlapInLocalDB:cryingInfo];
    XCTAssertEqual(ret, NO, @"check overlap crying Diary error by add! appId:%@", cryingInfo.appId);
    
    ret = [[DNCryingManager sharedInstance] addDiary:cryingInfo];
    XCTAssertEqual(ret, YES, "add crying Diary failed! appId:%@", cryingInfo.appId);
    
    ret = [[DNCryingManager sharedInstance] isOverlapInLocalDB:cryingInfo1];
    XCTAssertEqual(ret, NO, @"check overlap crying Diary error by add! appId:%@", cryingInfo1.appId);
    
    ret = [[DNCryingManager sharedInstance] addDiary:cryingInfo1];
    XCTAssertEqual(ret, YES, "add crying Diary failed! appId:%@", cryingInfo1.appId);
    
}

// 场景二：失败－ 新增时间冲突的model
-(void)test2AddbFail
{
    DNCryingModel *cryingDuplteInfo = [[DNCryingModel alloc] init];
    cryingDuplteInfo.appId =[NSString stringWithFormat:@"%f", [[NSDate date] timeIntervalSince1970]];
    cryingDuplteInfo.createTime =[NSString stringWithFormat:@"%.0f", [[NSDate date] timeIntervalSince1970]];
 
    //TODO: format to be confirm
    cryingDuplteInfo.ddat = [NSDate currentTimeString];
    cryingDuplteInfo.timeOfDay = TimeOfDay_Morning;
    cryingDuplteInfo.cryInyn = @"N";
    cryingDuplteInfo.crySttime = @"09:00";
    cryingDuplteInfo.cryEntime = @"11:00";
    
    BOOL ret = [[DNCryingManager sharedInstance] isOverlapInLocalDB:cryingDuplteInfo];
    XCTAssertEqual(ret, YES, @"Duplicate crying data can't be saved! appId:%@", cryingDuplteInfo.appId);
    
    cryingDuplteInfo.crySttime = @"10:00";
    cryingDuplteInfo.cryEntime = @"12:00";
    ret = [[DNCryingManager sharedInstance] isOverlapInLocalDB:cryingDuplteInfo];
    XCTAssertEqual(ret, YES, @"Duplicate crying data can't be saved! appId:%@", cryingDuplteInfo.appId);
}


-(void)test3ModifySuc
{
    // 场景三：成功－ 编辑model后进行存储
    cryingInfo.timeOfDay = TimeOfDay_Afternoon;
    cryingInfo.cryInyn = @"N";
    cryingInfo.crySttime = @"10:00";
    cryingInfo.cryEntime = @"11:00";
    cryingInfo.modifyReason = @"modify reason 1";
    
    BOOL ret = [[DNCryingManager sharedInstance] isOverlapInLocalDB:cryingInfo];
    if(!ret)
    {
        ret = [[DNCryingManager sharedInstance] modifyDiary:cryingInfo];
    }
     XCTAssertEqual(ret, YES, @"modify crying Diary failed! appId:%@", cryingInfo.appId);
}


-(void)test4ModifyFail4
{
    //场景四：失败－编辑model后时间冲突，存储失败
    cryingInfo.crySttime = @"10:00";
    cryingInfo.cryEntime = @"12:00";
    cryingInfo.modifyReason = @"modify reason 2";
    
    BOOL ret = [[DNCryingManager sharedInstance] isOverlapInLocalDB:cryingInfo];
    XCTAssertEqual(ret, YES, @"check overlap crying Diary error by modify! appId:%@", cryingInfo.appId);
    
}

-(void)test5DeleteSuc
{
    // 场景五：成功－删除model
    cryingInfo1.deleteReason = @"delete reason";
    
    BOOL ret = [[DNCryingManager sharedInstance] deleteDiary:cryingInfo1];
    XCTAssertEqual(ret, YES, @"delete crying Diary failed! appId:%@", cryingInfo1.appId);
  
}

-(void)test6SaveArr
{
    [[DNDataBase shareInstance] clearAllTableData];
    
    NSMutableArray *cryingArr = [NSMutableArray array];
    NSTimeInterval timeInterval =  [[NSDate date] timeIntervalSince1970];
    for(int i=0; i<24; i++)
    {
        DNCryingModel *cryingTempInfo = [[DNCryingModel alloc] init];
        cryingTempInfo.ddat = [NSDate currentTimeString];
        cryingTempInfo.timeOfDay = TimeOfDay_Afternoon;
        cryingTempInfo.cryInyn = @"Y";
        cryingTempInfo.crySttime =[NSString stringWithFormat:@"%02d:%02d",i, i];
        cryingTempInfo.cryEntime = [NSString stringWithFormat:@"%02d:%02d",i, i+15];
        
        cryingTempInfo.appId =[NSString stringWithFormat:@"%.0f", timeInterval - i];
        cryingTempInfo.createTime =[NSString stringWithFormat:@"%.0f", timeInterval - i];
        cryingTempInfo.status = [NSNumber numberWithInt:OPStatus_ADD];
        
        [cryingArr addObject: cryingTempInfo];
    }
    
    
    BOOL ret =[[DNCryingManager sharedInstance] saveCryingDairyArrToLocalDB:cryingArr];
    XCTAssertEqual(ret, YES, @"DNCryingManager-- saveCryingDairyArrToLocalDB error");
 
    NSMutableArray *dispalyArr = [[[DNCryingDao alloc] init] getAppDisplayObjectModel];
    XCTAssertEqual(dispalyArr.count, 24, @"DNCryingManager-- getCryingDairyArrToLocalDB error");
    
}

@end
